<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class account_model extends MY_Model
{
	/**
	 * 获取管理员列表
	 * 
	 * return array
	 */
	public function getAccountList()
	{
		$sql  = "SELECT `user`.*,role.`name` role_name from ym_admin_user as user ,ym_admin_role as role where `user`.roleid=role.id and `user`.isDel=0";
        $query = $this->db->query($sql);
		return $query->result_array();
	}

    //添加角色及权限
    public function setSystemPower($groupID,$data = array()){
        if($groupID){
            $this->db->where("id",$groupID);
            $res = $this->db->update("ym_admin_role",$data);
        }else{
            $res = $this->db->insert("ym_admin_role",$data);
        }
        return $res;
    }

    //根据id获取权限信息
    public function getPowerInfo($id){
        $this->db->where("id",$id);
        $query = $this->db->get("ym_admin_role");
        return $query->row_array();
    }
    //删除账号
    public function delAccountbyid($id){
        $this->db->where("id",$id);
        $res = $this->db->update("ym_admin_user",array("isDel"=>1));
        return $res;
    }

    //设置开启状态
    public function setStatus($id,$status){
        if(empty($id) && empty($status)){
            return false;
        }
        $this->db->where("id",$id);
        $res = $this->db->update("ym_admin_user",array("status"=>$status));
        return $res;
    }



    /**
     * 编辑管理员
     * @param number $id
     * @param array $data
     * @return int
     */
    public function edit_account($id=0, $data=array())
    {
        if($id){
            $this->db->where('id', $id);
            $res = $this->db->update('ym_admin_user', $data);
        }else{
            $res = $this->db->insert('ym_admin_user', $data);
        }


        return $res;
    }
    public function check_email($id,$email)
    {
        $where = "";
        if($id){
            $where = " and id<> {$id}";
        }
        $sql = "SELECT * FROM ym_admin_user WHERE  email = '$email'".$where;
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
    public function check_username($id,$username)
    {
        $where = "";
        if($id){
            $where = " and id<> {$id}";
        }
        $sql = "SELECT * FROM ym_admin_user WHERE username = '$username'".$where;
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

}

/* End of file codeinfo_model.php */
/* Location: ./application/models/luckydog/codeinfo_model.php */
